﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wedo.Recognition.Domain.Models;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Domain;
using Wedo.Service.Common;

using Wedo.Utility;
using Wedo.Recognition.Repository;


namespace Wedo.Recognition.Service
{
    /// <summary>
    /// 退款的逻辑处理
    /// </summary>
    public class RefundmentApplyService : BaseService<RefundmentApply>
    {
        public RefundmentApplyService(IUnitWork dbContext)
            : base(dbContext, dbContext.Set<RefundmentApply>())
        {
        }

        /// <summary>
        /// 添加退款信息
        /// </summary>
        public void AddRefundment(string applyMan, RefundmentApply apply)
        {

            BankData bdItem = _dbContext.Set<BankData>()
                .Find(apply.BankDataId);

            if (bdItem.ImportSAP == false)
            {
                throw new Exception("必须导入SAP成功才能退款！");
            }

            if (apply.Amount <= 0)
            {
                throw new Exception("退款金额必须大于0！");
            }
            //没有签出
            if (bdItem.HasChecked(applyMan)==false)
                throw new Exception(Wedo.Recognition.Resources.ErrorMessage.Bill_Record_exist);

            //超出银行金额
            if (bdItem.BankPaidIn < apply.Amount)
                throw new Exception(Wedo.Recognition.Resources.ErrorMessage.RefundAmount_Over);

            if (bdItem.RemainRefundment() < apply.Amount)
            {
                throw new Exception("超出了剩余可退款金额，您可以通过申请争议解决");
            }

            var MyCheck = bdItem.CheckRecord.FirstOrDefault(m => m.ApplyMan == applyMan);

            apply.Id = Guid.NewGuid();
            apply.Status = 1;
            apply.BankDataId = bdItem.Id;
            apply.ApplyMan = applyMan;
            apply.ApplyTime = DateTime.Now;
            apply.CheckRecordID = MyCheck.Id;
            _repository.Create(apply);
        }


        /// <summary>
        /// 批准退款请求
        /// </summary>
        /// <param name="agreedMan">批准人</param>
        /// <param name="applyItemId">退款请求编号</param>
        public void ApproveRefundment(string agreedMan, Guid applyItemId)
        {

            RefundmentApply applyItem = GetById(applyItemId);
            applyItem.CheckNull("退款申请记录不存在");

            BankData bdItem = _dbContext.Set<BankData>()
                .Find(applyItem.BankDataId);

            if (bdItem.GenerateRecieveVoucher == false)
            {
                throw new Exception("必须生成了收款凭证才退款！");
            }

            if (bdItem.RemainRefundment() < applyItem.Amount)
                throw new Exception("超出了剩余可退款金额。你可以申请争议处理.");

            //审批通过
            applyItem.ApprovalTime = DateTime.Now;
            applyItem.Status = (int)ApprovalStatus.Agree;
            applyItem.Approver = agreedMan;

            Commit();
        }

        /// <summary>
        /// 拒绝退款请求
        /// </summary>
        /// <param name="agreedMan">批准人</param>
        /// <param name="applyItemId">退款请求编号</param>
        public void RejectedRefundment(string agreedMan, Guid applyItemId)
        {
            RefundmentApply applyItem = GetById(applyItemId);
            applyItem.CheckNull ("退款申请记录不存在");
            BankData bdItem = _dbContext.Set<BankData>()
                .Find(applyItem.BankDataId);

            //审批通过
            applyItem.ApprovalTime = DateTime.Now;
            applyItem.Status = (int)ApprovalStatus.Reject;
            applyItem.Approver = agreedMan;


            Commit();
        }

        /// <summary>
        /// 撤消退款请求
        /// <para>删除申请记录、签出记录</para>
        /// </summary>
        /// <param name="applyMan">申请人</param>
        /// <param name="applyItemId">退款请求编号</param>
        public void CancelRefundment(string applyMan, Guid applyItemId)
        {

            RefundmentApply applyItem = GetById(applyItemId);

            if (applyItem.Status > 1)
                throw new Exception("退款已经被审批，不能取消");
            if (applyItem.ApplyMan != applyMan)
                throw new Exception(Wedo.Recognition.Resources.ErrorMessage.Cancle_Own_Records);

            _repository.Delete(applyItem);
            Commit();
        }

        /// <summary>
        /// 编辑更新退款信息
        /// </summary>
        /// <param name="applyItem"></param>
        public void EditRefundment(string applyMan, RefundmentApply applyItem)
        {
            RefundmentApply sourceApplyItem = GetById(applyItem.Id);


            Update(applyItem);

            Commit();
        }
    }
}

